package cn.neusoft.service.impl;


import cn.neusoft.mapper.BusinessMapper;
import cn.neusoft.query.BaseQuery;
import cn.neusoft.pojo.Business;
import cn.neusoft.service.IBusinessService;
import cn.neusoft.vo.ResultBean;
import cn.neusoft.vo.PageBean;
import com.alibaba.druid.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
@Service
public class BusinessService implements IBusinessService {
	@Autowired
	private BusinessMapper businessMapper;
	@Transactional
	@Override
	public ResultBean add(Business business) throws Exception {
		System.out.println("添加操作");
		if (business==null ||
				StringUtils.isEmpty(business.getBusinessAccount()) ||
				StringUtils.isEmpty(business.getBusinessName())){
			return ResultBean.ERROR(10001,"params null");
		}

		int i = businessMapper.add(business);
		return i>0?ResultBean.SUCCESS():ResultBean.ERROR("add business fail");

	}
	@Transactional
	@Override
	public ResultBean update(Business business) throws Exception {
		System.out.println("更新操作");
		if (business==null ||
				business.getBusinessId()==null ||
				StringUtils.isEmpty(business.getBusinessAccount()) ||
				StringUtils.isEmpty(business.getBusinessName())){
			return ResultBean.ERROR(10001,"params null");
		}
		int i = businessMapper.update(business);
		return i>0?ResultBean.SUCCESS():ResultBean.ERROR("update business fail");
	}
	@Transactional
	@Override
	public ResultBean delete(Integer id) throws Exception {
		System.out.println("删除操作");
		//参数校验
		if (id==null){
			return ResultBean.ERROR(10001,"params null");
		}

		int count = businessMapper.delete(id);
		return count>0?ResultBean.SUCCESS():ResultBean.ERROR("delete business fail");

	}
	@Transactional
	@Override
	public ResultBean getById(Integer id) throws Exception {
		System.out.println("根据Id查询");
		//参数校验
		if (id==null){
			return ResultBean.ERROR(10001,"params null");
		}
		Business business = businessMapper.getById(id);
		return business!=null?ResultBean.SUCCESS(business):ResultBean.ERROR("get business by id fail");

	}
	@Transactional
	@Override
	public ResultBean getAll() throws Exception {
		System.out.println("查询全部");
		List<Business> businessList = businessMapper.getAll();
		return businessList!=null?ResultBean.SUCCESS(businessList):ResultBean.ERROR("get all business fail");

	}
	@Transactional
	@Override
	public ResultBean getByPage(BaseQuery baseQuery) throws Exception {
		System.out.println("分页查询");
		PageBean<Business> pageBean=new PageBean<>();

		//查询总记录和查询总的页数
		int count=businessMapper.getCount(baseQuery);
		pageBean.setTotalRows(count);
		if(count%baseQuery.getPageCount()==0) {
			pageBean.setTotalPages(count/baseQuery.getPageCount());
		} else {
			pageBean.setTotalPages(count/baseQuery.getPageCount()+1);
		}

		//查询集合
		List<Business> list=new ArrayList<>();
		list=businessMapper.getByPage(baseQuery);
		list.forEach(System.out::println);
		//再查询完这个集合的时候，还要将这个集合返回到前面
		pageBean.setData(list);
		return ResultBean.SUCCESS(pageBean);
	}
	@Transactional
	@Override
	public ResultBean batchDel(List<Integer> ids) throws Exception {
		if(ids==null|| ids.size()==0) {
			return ResultBean.ERROR("params is null");
		}
		for(Integer id:ids) {
			int delete=businessMapper.delete(id);
			if(delete<1) {
				return ResultBean.ERROR("delete is failed");
			}
		}
		return ResultBean.SUCCESS();
	}
}
